package com.example.tao.mycar.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.RequestMobileCodeCallback;
import com.avos.avoscloud.UpdatePasswordCallback;
import com.example.tao.mycar.MyActionBarActivity;
import com.example.tao.mycar.MyApplication;
import com.example.tao.mycar.R;
import com.example.tao.mycar.activity.HomeActivity;

public class FindPasswordActivity extends MyActionBarActivity implements View.OnClickListener {

    private EditText phoneNumText, passwordText, identifyText;
    private Button nextButton, sendIdentifyButton;

    private String phoneNumStr, passwordStr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_find_password);
        setToolBar(getResources().getString(R.string.title_activity_forget_password), true);

        phoneNumText = (EditText) this.findViewById(R.id.input_phone);
        passwordText = (EditText)this.findViewById(R.id.input_password);
        identifyText = (EditText)this.findViewById(R.id.identify_num);

        nextButton = (Button)this.findViewById(R.id.next_step);
        sendIdentifyButton = (Button)this.findViewById(R.id.send_identify);

        nextButton.setOnClickListener(this);
        sendIdentifyButton.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.next_step :
                passwordStr = passwordText.getText().toString();
                String identifyStr = identifyText.getText().toString();
                if (TextUtils.isEmpty(identifyStr)) {
                    Toast.makeText(FindPasswordActivity.this, R.string.input_identify, Toast.LENGTH_LONG).show();
                } else if (TextUtils.isEmpty(passwordStr)) {
                    Toast.makeText(FindPasswordActivity.this, R.string.input_password, Toast.LENGTH_LONG).show();
                } else {
                    checkIdentifyAndResetPassword(identifyStr, passwordStr);
                }

                break;
            case R.id.send_identify :
                phoneNumStr = phoneNumText.getText().toString();

                if (TextUtils.isEmpty(phoneNumStr)) {
                    Toast.makeText(FindPasswordActivity.this, R.string.input_phone_number, Toast.LENGTH_SHORT).show();
                    return;
                } else if (!phoneNumStr.matches("^(13|14|15|17|18)\\d{9}$")) {
                    Toast.makeText(FindPasswordActivity.this, R.string.input_legal_phone_number, Toast.LENGTH_LONG).show();
                    return;
                } else {
                    requestPassword(phoneNumStr);
                }

                break;
            default:
                break;
        }
    }

    private void checkIdentifyAndResetPassword(String identifyStr, String passwordStr) {
        AVUser.resetPasswordBySmsCodeInBackground(identifyStr, passwordStr, new UpdatePasswordCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    if (AVUser.getCurrentUser() != null) {
                        Intent intent = new Intent(FindPasswordActivity.this, HomeActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(FindPasswordActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }
                } else {
                    if (e.getCode() == 603) {
                        Toast.makeText(FindPasswordActivity.this, R.string.smsCodeError, Toast.LENGTH_LONG).show();
                    }
                }
            }
        });
    }

    private void requestPassword(String phoneNumStr) {
        AVUser.requestPasswordResetBySmsCodeInBackground(phoneNumStr, new RequestMobileCodeCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    Toast.makeText(FindPasswordActivity.this, "验证码已经发送", Toast.LENGTH_LONG).show();
                } else {
                    if (e.getCode() == 213) {
                        Toast.makeText(FindPasswordActivity.this, "该用户名未注册", Toast.LENGTH_LONG).show();
                    }
                }
            }
        });
    }
}
